package com.yulinlin.jdbc.mysql;

import com.yulinlin.core.wrapper.IDeleteWrapper;
import com.yulinlin.core.wrapper.IParamsWrapper;
import com.yulinlin.core.wrapper.IStringConditionWrapper;

public class MysqlDeleteWrapper<E,R> implements IDeleteWrapper<E,R> {

    private String tableName;

    private IStringConditionWrapper<E,IDeleteWrapper<E,R> > whereWrapper;

    private R endWrapper;

    public MysqlDeleteWrapper(R endWrapper) {
        this.endWrapper = endWrapper;
        this.whereWrapper = new MysqlStringConditionWrapper(this);
    }

    @Override
    public IStringConditionWrapper<E,IDeleteWrapper<E,R> > where() {
        return this.whereWrapper;
    }

    @Override
    public IDeleteWrapper table(String tableName) {
        this.tableName = tableName;
        return this;
    }

    @Override
    public String getSql() {
        StringBuffer sb =  new StringBuffer();
        sb.append("delete from ");
        sb.append(tableName);
        String where =  whereWrapper.getSql();
        if(where.length() > 0){
            sb.append(" where ");
            sb.append(where);
        }
        return sb.toString();
    }

    @Override
    public R end() {
        return endWrapper;
    }

    @Override
    public IParamsWrapper getParams() {
        return this.whereWrapper.getParams();
    }

}
